package cn.signalpha.modules.shop.controller.seller;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.shop.dto.PatternDTO;
import cn.signalpha.modules.shop.entity.Pattern;
import cn.signalpha.modules.shop.entity.enums.PatternTypeEnum;
import cn.signalpha.modules.shop.service.PatternService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/sys-seller/pattern")
@Api(value = "运营模式", tags = "运营模式")
public class SpiPatternController extends BaseController {

	private final PatternService patternService;

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "")
	public R info() {
		//给商户创建分销模式
		Long sellerId = Long.valueOf(getUser().getTenantId());
		Pattern pattern = patternService.getBySellerIdAndType(sellerId, PatternTypeEnum.DISTRIBUTION.getType());
		if(ObjectUtil.isEmpty(pattern)){
			pattern = new Pattern();
			pattern.setSellerId(sellerId);
			pattern.setType(PatternTypeEnum.DISTRIBUTION.getType());
			pattern.setTitle(PatternTypeEnum.DISTRIBUTION.getName());
			pattern.setCreateTime(new Date());
			patternService.save(pattern);
		}
		return R.data(pattern);
	}

	@PostMapping("/update")
	@ApiOperation(value = "设置分销规则", notes = "")
	public R update(@Valid @RequestBody PatternDTO.UpdateDTO updateDTO) {
		return R.status(patternService.updateById(BeanUtil.copy(updateDTO, Pattern.class)));
	}

}
